// 本地存储工具函数

// 存储键名常量
export const STORAGE_KEYS = {
  TOKEN: 'user_token',
  USER_INFO: 'user_info',
  PHONE: 'user_phone',
  LOGIN_TIME: 'login_time',
  WITHDRAW_BALANCE: 'withdraw_balance', // 提现页面隐式传参-可提现余额
  WITHDRAW_ALIPAY: 'withdraw_alipay' // 新增：提现页面隐式传参-支付宝账号
};

// 设置存储项
export const setStorage = (key, value) => {
  try {
    const data = typeof value === 'object' ? JSON.stringify(value) : value;
    uni.setStorageSync(key, data);
  } catch (error) {
    console.error('设置存储失败:', error);
  }
};

/**
 * 获取存储项
 * @param {string} key 键名
 * @param {any} defaultValue 默认值
 * @returns {any} 存储的值
 */
export const getStorage = (key, defaultValue = null) => {
  try {
    const data = uni.getStorageSync(key);
    if (!data) return defaultValue;
    
    // 尝试解析JSON
    try {
      return JSON.parse(data);
    } catch {
      return data;
    }
  } catch (error) {
    console.error('获取存储失败:', error);
    return defaultValue;
  }
};

/**
 * 移除存储项
 * @param {string} key 键名
 */
export const removeStorage = (key) => {
  try {
    uni.removeStorageSync(key);
  } catch (error) {
    console.error('移除存储失败:', error);
  }
};

/**
 * 清空所有存储
 */
export const clearStorage = () => {
  try {
    uni.clearStorageSync();
  } catch (error) {
    console.error('清空存储失败:', error);
  }
};

// 用户相关存储操作
export const userStorage = {
  // 设置用户token
  setToken: (token) => {
    console.log('💾 设置新Token:', token);
    setStorage(STORAGE_KEYS.TOKEN, token);
    setStorage(STORAGE_KEYS.LOGIN_TIME, Date.now());
  },
  
  // 获取用户token
  getToken: () => {
    const token = getStorage(STORAGE_KEYS.TOKEN);
    console.log('🔑 当前Token:', token || '未登录');
    return token;
  },
  
  // 移除用户token
  removeToken: () => {
    console.log('🗑️ 移除Token');
    removeStorage(STORAGE_KEYS.TOKEN);
    removeStorage(STORAGE_KEYS.LOGIN_TIME);
  },
  
  // 设置用户信息
  setUserInfo: (userInfo) => {
    setStorage(STORAGE_KEYS.USER_INFO, userInfo);
  },
  
  // 获取用户信息
  getUserInfo: () => {
    return getStorage(STORAGE_KEYS.USER_INFO, {});
  },
  
  // 移除用户信息
  removeUserInfo: () => {
    removeStorage(STORAGE_KEYS.USER_INFO);
  },
  
  // 设置用户手机号
  setPhone: (phone) => {
    setStorage(STORAGE_KEYS.PHONE, phone);
  },
  
  // 获取用户手机号
  getPhone: () => {
    return getStorage(STORAGE_KEYS.PHONE);
  },
  
  // 检查是否已登录
  isLoggedIn: () => {
    const token = getStorage(STORAGE_KEYS.TOKEN);
    const loginTime = getStorage(STORAGE_KEYS.LOGIN_TIME);
    
    if (!token || !loginTime) return false;
    
    // 检查token是否过期（假设7天过期）
    const expireTime = 7 * 24 * 60 * 60 * 1000; // 7天
    const now = Date.now();
    
    if (now - loginTime > expireTime) {
      // token过期，清除存储
      userStorage.logout();
      return false;
    }
    
    return true;
  },
  
  // 登出，清除所有用户相关存储
  logout: () => {
    removeStorage(STORAGE_KEYS.TOKEN);
    removeStorage(STORAGE_KEYS.USER_INFO);
    removeStorage(STORAGE_KEYS.PHONE);
    removeStorage(STORAGE_KEYS.LOGIN_TIME);
  }
};

export default {
  setStorage,
  getStorage,
  removeStorage,
  clearStorage,
  userStorage,
  STORAGE_KEYS
};